package com.example.studentapp.money;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.studentapp.MainActivity;
import com.example.studentapp.R;
import com.example.studentapp.db.SQLiteDB;

import java.text.SimpleDateFormat;
import java.util.Date;

public class AddMoneyActivity extends AppCompatActivity {
    ImageView money_add_returnIV;
    TextView money_add_saveTV;
    EditText money_clothET,money_foodET,money_houseET,money_useET,money_walkET,money_otherET,money_getET; 
    
    SQLiteDB sqLiteDB;
    SQLiteDatabase sqLiteDatabase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_money);
        init();

        money_add_returnIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        money_add_saveTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String cloth=money_clothET.getText().toString();
                String food=money_foodET.getText().toString();
                String house=money_houseET.getText().toString();
                String use=money_useET.getText().toString();
                String walk=money_walkET.getText().toString();
                String other=money_otherET.getText().toString();
                String get=money_getET.getText().toString();
                if(cloth.length()>0||food.length()>0||house.length()>0||use.length()>0
                        ||walk.length()>0||other.length()>0||get.length()>0){
                    // 插入数据
                    try{
                        sqLiteDB = new SQLiteDB(getApplicationContext());
                        sqLiteDatabase = sqLiteDB.getWritableDatabase();
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                        String record_date = simpleDateFormat.format(new Date());
                        String insertSql="insert into money(cloth,food,house,use,walk,other,get_money,record_date)" +
                                " values("+getValue(cloth)+","+getValue(food)+","
                                +getValue(house)+","+getValue(use)+","+getValue(walk)
                                +","+getValue(other)+","+getValue(get)+",'"+record_date+"')";
                        sqLiteDatabase.execSQL(insertSql);
                        Intent intent = new Intent(AddMoneyActivity.this, MainActivity.class);
                        intent.putExtra("index",2);
                        startActivity(intent);
                        finish();
                    }catch (Exception e){
                        Toast.makeText(getApplicationContext(), getString(R.string.money_add_error), Toast.LENGTH_SHORT).show();
                    }

                }
            }


        });
    }

    private double getValue(String moneys) {
        double money = 0.0;
        if(moneys.length()>0){
            money = Double.parseDouble(moneys);
        }
        return money;

    }


    private void init() {
        money_add_returnIV = findViewById(R.id.money_add_returnIV);
        money_add_saveTV = findViewById(R.id.money_add_saveTV);
        money_clothET = findViewById(R.id.money_clothET);
        money_foodET = findViewById(R.id.money_foodET);
        money_houseET = findViewById(R.id.money_houseET);
        money_useET = findViewById(R.id.money_useET);
        money_walkET = findViewById(R.id.money_walkET);
        money_otherET = findViewById(R.id.money_otherET);
        money_getET = findViewById(R.id.money_getET);
    }
}